package com.ghh.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ghh.constant.MessageConstant;
import com.ghh.entity.PageResult;
import com.ghh.entity.QueryPageBean;
import com.ghh.entity.Result;
import com.ghh.pojo.Role;
import com.ghh.service.MenuService;
import com.ghh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.core.userdetails.User;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference //查找服务
    private UserService userService;
    @Reference
    private MenuService menuService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @RequestMapping("/getUsername")
    public Result getUsername(){
        //当Spring security完成认证后，会将当前用户信息保存到框架提供的上下文对象
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(user != null){
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

    //查询用户管理分页操作，并条件查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            return userService.findPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //查询角色管理表
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> list=userService.findAll();
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,list);//查询用户管理成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_USER_LIST_FAIL);//查询用户管理失败
        }
    }

    //添加用户
    @RequestMapping("/add")
    public Result add( Integer[] roleIds,@RequestBody com.ghh.pojo.User user){
        String password = passwordEncoder.encode(user.getPassword());
        user.setPassword(password);
        try {
            userService.add(roleIds,user);
            return new Result(true, MessageConstant.ADD_USER_SUCCESS);//添加用户成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_USER_FALL);//添加用户失败
        }
    }

    //根据ID查询用户
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            com.ghh.pojo.User user = userService.findById(id);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS ,user);//查询用户数据成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_LIST_FAIL);//查询用户数据失败
        }
    }

    //根据用户id查询 关联的角色 id
    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer id){
        try {
            List<Integer> roleIds = userService.findRoleIdsByUserId(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,roleIds);//查询角色列表成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);//查询角色列表数据失败
        }
    }

    //编辑用户和角色表
    @RequestMapping("/edit")
    public Result edit(@RequestBody com.ghh.pojo.User user, Integer[] roleIds){
        //密码加密
        String password = passwordEncoder.encode(user.getPassword());
        user.setPassword(password);
        try {
            userService.edit(roleIds,user);
            return new Result(true, MessageConstant.EDIT_USER_SUCCESS);//编辑用户数据成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_USER_FALL);//编辑用户数据失败
        }
    }

    //删除
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            userService.delete(id);
            return new Result(true,MessageConstant.DELETE_USER_SUCCESS);//删除用户成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_USER_FAIL);//删除用户失败
        }
    }

}
